package the.broccoli.market.niche.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.web.BusinessException;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.market.niche.infrastructure.mapper.NicheContentMapper;
import the.broccoli.market.niche.infrastructure.model.NicheContentDO;
import the.broccoli.market.niche.interfaces.controller.req.*;
import the.broccoli.market.niche.interfaces.controller.resp.NicheContentItemResp;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NicheContentService {

    @Resource
    private NicheContentMapper nicheContentMapper;

    public boolean add(NicheContentAddReq req) {
        NicheContentDO entity = new NicheContentDO();
        entity.setContent(req.getContent());
        entity.setIfOpen(0);
        entity.setType(req.getType());
        entity.setPositionId(req.getPositionId());
        return nicheContentMapper.insert(entity) > 0;
    }

    public boolean modify(NicheContentModifyReq req) {
        NicheContentDO entity = new NicheContentDO();
        entity.setId(req.getId());
        entity.setContent(req.getContent());
        entity.setIfOpen(req.getIfOpen());
        return nicheContentMapper.updateById(entity) > 0;
    }

    public QueryResult<NicheContentItemResp> page(NicheContentPageReq req) {
        if (req.getPageNum() != null && req.getPageSize() != null) {
            PageHelper.startPage(req.getPageNum(), req.getPageSize());
        }

        List<NicheContentItemResp> dataList = new ArrayList<>();
        Page<NicheContentDO> page = nicheContentMapper.selectListPage(req);
        if (!CollectionUtils.isEmpty(page.getResult())) {
            dataList = page.getResult().stream().map(this::convertOf).collect(Collectors.toList());
        }
        return new QueryResult<NicheContentItemResp>(page.getTotal(), dataList);
    }

    public NicheContentItemResp get(Integer id) {
        NicheContentDO content = nicheContentMapper.selectById(id);
        return content == null ? null : convertOf(content);
    }

    public boolean open(NicheContentOpenReq req) {
        if (req.getIfOpen() == 1) {
            NicheContentDO entity = nicheContentMapper.selectById(req.getId());
            if (entity == null) {
                return false;
            }
            NicheContentCurrentReq currentReq = new NicheContentCurrentReq();
            currentReq.setPositionId(entity.getPositionId());
            NicheContentItemResp content = getCurrent(currentReq);
            if (content != null) {
                throw BusinessException.withErrorCode("只能开启一项内容，请先关闭再开启");
            }
        }
        return nicheContentMapper.update(null, new LambdaUpdateWrapper<NicheContentDO>()
                .set(NicheContentDO::getModifiedTime, new Date())
                .set(NicheContentDO::getIfOpen, req.getIfOpen())

                .eq(NicheContentDO::getId, req.getId())
        ) > 0;
    }

    public NicheContentItemResp getCurrent(NicheContentCurrentReq req) {
        List<NicheContentDO> dataList = nicheContentMapper.selectList(new LambdaQueryWrapper<NicheContentDO>()
                .eq(NicheContentDO::getIfOpen, 1)
                .eq(NicheContentDO::getPositionId, req.getPositionId())
        );
        return CollectionUtils.isEmpty(dataList) ? null : convertOf(dataList.get(0));
    }

    private NicheContentItemResp convertOf(NicheContentDO source) {
        NicheContentItemResp target = new NicheContentItemResp();
        target.setId(source.getId());
        target.setContent(source.getContent());
        target.setIfOpen(source.getIfOpen());
        return target;
    }


}
